package com.example.article.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.apis.article.iArticleClient;
import com.example.article.mapper.ApArticleMapper;
import com.example.article.mapper.ApArticleThumbMapper;
import com.example.article.service.ApArticleService;
import com.example.model.article.dtos.ArticleDto;
import com.example.model.article.pojos.ApArticle;
import com.example.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ArticleClient implements iArticleClient {

    @Autowired
    private ApArticleService apArticleService;

    @PostMapping("/feign/v1/article/save")
    @Override
    public ResponseResult saveArticle(@RequestBody ArticleDto dto) {
        return apArticleService.saveArticle(dto);
    }

    @Override
    @PostMapping("/feign/v1/article/addCommentsNum")
    public ResponseResult addCommentsNum(String articleId) {
        ApArticle apArticle = apArticleService.getById(articleId);
        apArticle.setComment(apArticle.getComment()+1);
        apArticleService.updateById(apArticle);
        return ResponseResult.okResult("评论数+1");
    }

    @Autowired
    private ApArticleThumbMapper apArticleThumbMapper;

    @Override
    @PostMapping("/feign/v1/article/getCommentsNum")
    public ResponseResult getCommentsNum(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",id);

        return ResponseResult.okResult(apArticleThumbMapper.selectCount(queryWrapper));
    }

    @Autowired
    private ApArticleMapper apArticleMapper;

    @PostMapping("/feign/v1/article/getArticleByAclId")
    @Override
    public ResponseResult getArticleByAclId(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("author_id",id);

        return ResponseResult.okResult(apArticleMapper.selectList(queryWrapper));
    }

    @Override
    @PostMapping("/feign/v1/article/getArticleByArticleId")
    public ResponseResult getArticleByArticleId(String articleId) {
        return ResponseResult.okResult(apArticleMapper.selectById(articleId));
    }
}
